package com.ruoyi.mall.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.service.IMallCouponService;
import com.ruoyi.mall.service.IMallMemberCouponService;
import com.ruoyi.mall.service.IMallMemberUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.service.IMallGiveCouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 赠送优惠券Controller
 *
 * @author ruoyi
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/mall/mallGiveCoupon")
public class MallGiveCouponController extends BaseController
{
    @Autowired
    private IMallGiveCouponService mallGiveCouponService;
    @Autowired
    private IMallCouponService mallCouponService;
    @Autowired
    private IMallMemberUserService mallMemberUserService;
    @Autowired
    private IMallMemberCouponService mallMemberCouponService;

    /**
     * 查询赠送优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallGiveCoupon mallGiveCoupon)
    {
        startPage();
        List<MallGiveCoupon> list = mallGiveCouponService.selectMallGiveCouponList(mallGiveCoupon);
        return getDataTable(list);
    }

    /**
     * 导出赠送优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:export')")
    @Log(title = "赠送优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallGiveCoupon mallGiveCoupon)
    {
        List<MallGiveCoupon> list = mallGiveCouponService.selectMallGiveCouponList(mallGiveCoupon);
        ExcelUtil<MallGiveCoupon> util = new ExcelUtil<MallGiveCoupon>(MallGiveCoupon.class);
        return util.exportExcel(list, "赠送优惠券数据");
    }

    /**
     * 获取赠送优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallGiveCouponService.selectMallGiveCouponById(id));
    }

    /**
     * 新增赠送优惠券
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:add')")
    @Log(title = "赠送优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallGiveCoupon mallGiveCoupon)
    {
        MallCoupon coupon = mallCouponService.selectMallCouponById(mallGiveCoupon.getCouponId());
        MallMemberUser user = mallMemberUserService.selectMallMemberUserById(mallGiveCoupon.getUserId());
        if (user != null && coupon != null) {
            MallMemberCoupon memberCoupon = new MallMemberCoupon();
            memberCoupon.setUserId(mallGiveCoupon.getUserId());
            memberCoupon.setUserName(user.getName());
            memberCoupon.setCouponId(mallGiveCoupon.getCouponId());
            memberCoupon.setCouponName(coupon.getName());
            memberCoupon.setCouponType("后台赠送");
            memberCoupon.setCreateAt(DateUtils.getTime());
            memberCoupon.setIsUsed("0");
            memberCoupon.setPrice(coupon.getPrice());
            mallMemberCouponService.insertMallMemberCoupon(memberCoupon);
        }
        return toAjax(mallGiveCouponService.insertMallGiveCoupon(mallGiveCoupon));
    }

    /**
     * 修改赠送优惠券
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:edit')")
    @Log(title = "赠送优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallGiveCoupon mallGiveCoupon)
    {
        return toAjax(mallGiveCouponService.updateMallGiveCoupon(mallGiveCoupon));
    }

    /**
     * 删除赠送优惠券
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGiveCoupon:remove')")
    @Log(title = "赠送优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallGiveCouponService.deleteMallGiveCouponByIds(ids));
    }

    /**
     * 查询优惠券列表
     */
    @GetMapping("/getCouponList")
    public AjaxResult getCouponList(MallCoupon mallCoupon)
    {
        mallCoupon.setDelFlag("0");
        mallCoupon.setIsFiring("0");
        List<MallCoupon> list = mallCouponService.selectMallCouponList(mallCoupon);
        return AjaxResult.success(list);
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/getUserList")
    public AjaxResult getUserList(MallMemberUser mallMemberUser)
    {
        mallMemberUser.setDelFlag("0");
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        for (MallMemberUser MMU : list) {
            MMU.setName(MMU.getName()+"--"+MMU.getPhone());
        }
        return AjaxResult.success(list);
    }
}
